package com.store.framework.db;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.sql.rowset.CachedRowSet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.store.framework.dao.IAppAdminDao;
import com.store.framework.entry.AppAdmin;

public class AppAdminDao implements IAppAdminDao {
	private static Log LOG = LogFactory.getLog(AppAdminDao.class);


	public List<AppAdmin> select(Long start , Long limit) {
		List<AppAdmin> list = new ArrayList<AppAdmin>();
		String sql ="select * from App_Admin limit "+start +"," +limit;
		try {
			CachedRowSet cr =DaoUtil.SimpleQuery(sql);
			while(cr.next()){
				AppAdmin appadmin =new AppAdmin();
				appadmin.setId(cr.getString("Id"));
				appadmin.setAdName(cr.getString("AdName"));
				appadmin.setAdCode(cr.getString("AdCode"));
				appadmin.setAdPassword(cr.getString("AdPassword"));
				appadmin.setRegDate(cr.getDate("RegDate")==null?null:cr.getDate("RegDate").getTime());
				appadmin.setRegSex(cr.getString("RegSex"));
				appadmin.setRegbirthday(cr.getDate("Regbirthday")==null?null:new SimpleDateFormat("yyyy-MM-dd").format(cr.getDate("Regbirthday")));
				appadmin.setRegResaddres(cr.getString("RegResaddres"));
				appadmin.setRegHomeTaddres(cr.getString("RegHomeTaddres"));
				appadmin.setRegConstellation(cr.getString("RegConstellation"));
				appadmin.setPerState(cr.getByte("PerState"));
				appadmin.setCancellation(cr.getByte("Cancellation"));
				appadmin.setAddDate(cr.getDate("SAddDate")==null?null:cr.getDate("SAddDate").getTime());
				appadmin.setSaddPer(cr.getString("SaddPer"));
				appadmin.setEditDate(cr.getDate("EditDate")==null?null:cr.getDate("EditDate").getTime());
				appadmin.setEditPer(cr.getString("EditPer"));
				appadmin.setDelDate(cr.getDate("DelDate")==null?null:cr.getDate("DelDate").getTime());
				appadmin.setDelPer(cr.getString("DelPer"));
				appadmin.setDel(cr.getByte("Del"));
				appadmin.setRemarks(cr.getString("Remarks"));
				list.add(appadmin);
				
			}
		} catch (SQLException e) {
			list =null;
			LOG.error(e.getMessage(),e);
			throw new RuntimeException(e.getMessage(),e);
		}
		
		return list;
	}

	public List<AppAdmin> select() {
		List<AppAdmin> list = new ArrayList<AppAdmin>();
		String sql ="select * from App_Admin ";
		try {
			CachedRowSet cr =DaoUtil.SimpleQuery(sql);
			while(cr.next()){
				AppAdmin appadmin =new AppAdmin();
				appadmin.setId(cr.getString("Id"));
				appadmin.setAdName(cr.getString("AdName"));
				appadmin.setAdCode(cr.getString("AdCode"));
				appadmin.setAdPassword(cr.getString("AdPassword"));
				appadmin.setRegDate(cr.getDate("RegDate")==null?null:cr.getDate("RegDate").getTime());
				appadmin.setRegSex(cr.getString("RegSex"));
				appadmin.setRegbirthday(cr.getDate("Regbirthday")==null?null:new SimpleDateFormat("yyyy-MM-dd").format(cr.getDate("Regbirthday")));
				appadmin.setRegResaddres(cr.getString("RegResaddres"));
				appadmin.setRegHomeTaddres(cr.getString("RegHomeTaddres"));
				appadmin.setRegConstellation(cr.getString("RegConstellation"));
				appadmin.setPerState(cr.getByte("PerState"));
				appadmin.setCancellation(cr.getByte("Cancellation"));
				appadmin.setAddDate(cr.getDate("SAddDate")==null?null:cr.getDate("SAddDate").getTime());
				appadmin.setSaddPer(cr.getString("SaddPer"));
				appadmin.setEditDate(cr.getDate("EditDate")==null?null:cr.getDate("EditDate").getTime());
				appadmin.setEditPer(cr.getString("EditPer"));
				appadmin.setDelDate(cr.getDate("DelDate")==null?null:cr.getDate("DelDate").getTime());
				appadmin.setDelPer(cr.getString("DelPer"));
				appadmin.setDel(cr.getByte("Del"));
				appadmin.setRemarks(cr.getString("Remarks"));
				list.add(appadmin);
				
			}
		} catch (SQLException e) {
			list =null;
			LOG.error(e.getMessage(),e);
			throw new RuntimeException(e.getMessage(),e);
		}
		
		return list;
	}
	
	@Override
	public int Add(AppAdmin appad) {
		int admin = 0;
		String sql ="insert into app_admin(Id,AdName,AdCode,AdPassword,"
				+ "RegDate,RegSex,Regbirthday,RegResaddres,RegHomeTaddres,"
				+ "RegConstellation,PerState,Cancellation,"
				+ "SAddDate,SaddPer,EditDate,EditPer,DelDate,DelPer,Del,Remarks)"
				+ " value(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
	    try {
			DaoUtil.executeUpdate(sql, appad.getId(),appad.getAdName(),appad.getAdCode(),
					appad.getAdPassword(),appad.getRegDate(),appad.getRegSex(),
					appad.getRegbirthday(),appad.getRegResaddres(),appad.getRegHomeTaddres(),
					appad.getRegConstellation(),appad.getPerState(),appad.getCancellation(),
					appad.getAddDate(),appad.getSaddPer(),appad.getEditDate(),appad.getEditPer(),
					appad.getDelDate(),appad.getDelPer(),appad.getDel(),appad.getRemarks());
			admin=1;
		} catch (Exception e) {
			admin=0;
			LOG.error(e.getMessage(),e);
			throw new RuntimeException(e.getMessage(),e);
		}
		return admin;
	}
	
	@Override
	public int delete(AppAdmin appad){
		int admin = 0;
		String sql ="delete from app_admin where id = ?";
	    try {
			DaoUtil.executeUpdate(sql, appad.getId());
			admin=1;
		} catch (Exception e) {
			admin=0;
			LOG.error(e.getMessage(),e);
			throw new RuntimeException(e.getMessage(),e);
		}
		return admin;
	}

	@Override
	public int update(AppAdmin appad){
		int admin = 1;
		String sql ="update app_admin set AdName=?,AdCode=?,AdPassword=?,"
				+ "RegDate=?,RegSex=?,Regbirthday=?,RegResaddres=?,RegHomeTaddres=?,"
				+ "RegConstellation=?,PerState=?,Cancellation=?,"
				+ "SAddDate=?,SaddPer=?,EditDate=?,EditPer=?,DelDate=?,DelPer=?,Del=?,Remarks=?"
				+ " where id = ?";
	    try {
			DaoUtil.executeUpdate(sql, appad.getAdName(),appad.getAdCode(),
					appad.getAdPassword(),appad.getRegDate(),appad.getRegSex(),
					appad.getRegbirthday(),appad.getRegResaddres(),appad.getRegHomeTaddres(),
					appad.getRegConstellation(),appad.getPerState(),appad.getCancellation(),
					appad.getAddDate(),appad.getSaddPer(),appad.getEditDate(),appad.getEditPer(),
					appad.getDelDate(),appad.getDelPer(),appad.getDel(),appad.getRemarks(),
					appad.getId());
			admin=1;
		} catch (Exception e) {
			admin=0;
			LOG.error(e.getMessage(),e);
			throw new RuntimeException(e.getMessage(),e);
		}
		return admin;
	}
	
	public int getCount() {
		int count = 0;
		String sql = "select count(0) from App_Admin";

		try {
			CachedRowSet cr = DaoUtil.SimpleQuery(sql);
			if (cr.next()) {
				count = cr.getInt(1);
			}
		} catch (SQLException e) {
			count = 0;
			LOG.error(e.getMessage(),e);
			throw new RuntimeException(e.getMessage(),e);
		}
		return count;
	}

}
